package net.mikoo.seals.api.model.use;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import net.mikoo.seals.api.model.BaseResponse;
import net.mikoo.seals.api.model.apply.ApplyDetailInfo;
import net.mikoo.seals.api.model.apply.ApplyDetailInfo.Voucher;
import net.mikoo.seals.api.model.devices.DevicesInfo;
import net.mikoo.seals.api.model.seal.SealInfo;
import net.mikoo.seals.api.utility.ApiConstants;
import net.mikoo.seals.common.enums.ApplyStatusEnum;
import net.mikoo.seals.common.enums.ApplyTypeEnum;
import net.mikoo.seals.common.enums.DevicesStatusEnum;
import net.mikoo.seals.common.enums.SealStatusEnum;
import net.mikoo.seals.common.enums.TakeStatusEnum;
import net.mikoo.seals.model.Apply;
import net.mikoo.seals.model.DevicesAct;
import net.mikoo.seals.model.DevicesTake;
import net.mikoo.seals.model.Upfile;

public class GetWaitUseDetailResponse extends BaseResponse {

	private static final long serialVersionUID = -5298857578258448859L;

	private Long id;
	
	private String devicesName;
	
	private Integer total;
	
	private Integer surplus;
	
	private String type;
	
	private String startTime;
	
	private String expireTime;
	
	private Long partyId;
	
	private SealInfo sealInfo;
	
	private ApplyDetailInfo apply;
	
	private DevicesInfo devicesInfo;
	
	private String status;
	
	private String statusName;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getDevicesName() {
		return devicesName;
	}

	public void setDevicesName(String devicesName) {
		this.devicesName = devicesName;
	}

	public Integer getTotal() {
		return total;
	}

	public void setTotal(Integer total) {
		this.total = total;
	}

	public Integer getSurplus() {
		return surplus;
	}

	public void setSurplus(Integer surplus) {
		this.surplus = surplus;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getExpireTime() {
		return expireTime;
	}

	public void setExpireTime(String expireTime) {
		this.expireTime = expireTime;
	}
	
	public Long getPartyId() {
		return partyId;
	}

	public void setPartyId(Long partyId) {
		this.partyId = partyId;
	}

	public SealInfo getSealInfo() {
		return sealInfo;
	}

	public void setSealInfo(SealInfo sealInfo) {
		this.sealInfo = sealInfo;
	}

	public ApplyDetailInfo getApply() {
		return apply;
	}

	public void setApply(ApplyDetailInfo apply) {
		this.apply = apply;
	}
	
	public DevicesInfo getDevicesInfo() {
		return devicesInfo;
	}

	public void setDevicesInfo(DevicesInfo devicesInfo) {
		this.devicesInfo = devicesInfo;
	}
	
	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getStatusName() {
		return statusName;
	}

	public void setStatusName(String statusName) {
		this.statusName = statusName;
	}

	public static GetWaitUseDetailResponse valueOf(DevicesTake take) {
		GetWaitUseDetailResponse result = new GetWaitUseDetailResponse();
		
		if(take == null) {
			return result;
		}
		
		result.setId(take.getDevicesTakeId());
		result.setType(ApplyTypeEnum.TAKE_APPLY.getCode());
		result.setPartyId(take.getPartyId());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		if(take.getStartDate() != null) {
			result.setStartTime(sdf.format(take.getStartDate()));
		}
		if(take.getEndDate() != null) {
			result.setExpireTime(sdf.format(take.getEndDate()));
		}
		
		if(take.getSeal() != null) {
			SealInfo sealInfo = new SealInfo();
			sealInfo.setSealId(take.getSeal().getSealId());
			sealInfo.setSealName(take.getSeal().getSealName());
			if(StringUtils.isNotBlank(take.getSeal().getSealName())) {
				sealInfo.setIconText(take.getSeal().getSealName().length() > 2 ? take.getSeal().getSealName().substring(0, 2) : take.getSeal().getSealName().substring(0, 1));
			}
			
			SealStatusEnum sealStatus = SealStatusEnum.getByCode(take.getSeal().getSealStatus());
			if(sealStatus != null){
				sealInfo.setStatusName(sealStatus.getMessage());
			}
			
			sealInfo.setCustosPartyName(take.getSeal().getCustosName());
			sealInfo.setSealStatus(take.getSeal().getSealStatus());
			result.setSealInfo(sealInfo);
		}
		
		Apply apply = take.getApply();
		if(apply != null) {
			ApplyDetailInfo applyInfo = new ApplyDetailInfo();
			
			applyInfo.setPartyName(apply.getPerson().getName());
			applyInfo.setCause(apply.getCause());
			
			ApplyStatusEnum applyStatus = ApplyStatusEnum.getByCode(apply.getApplyStatus());
			if(applyStatus != null) {
				applyInfo.setStatus(applyStatus.getCode());
				applyInfo.setStatusName(applyStatus.getMessage());
			}
			applyInfo.setTimes(apply.getApplyTimes());
			
			ApplyTypeEnum applyType = ApplyTypeEnum.getByCode(apply.getApplyType());
			
			applyInfo.setApplyType(apply.getApplyType());
			if(applyType != null) {
				applyInfo.setApplyTypeName(applyType.getMessage());
			}
			
			if(apply.getApplyTime() != null) {
				applyInfo.setApplyTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(apply.getApplyTime()));
			}
			
			if(apply.getUpfiles() != null) {
				List<Voucher> vouchers = new ArrayList<Voucher>();
				
				for(Upfile upfile : apply.getUpfiles()) {
					if(upfile == null || StringUtils.isBlank(upfile.getFileUrl())) {
						continue;	
					}
					Voucher voucher = new Voucher();
					voucher.setUrl(upfile.getFileUrl());
					voucher.setId(upfile.getUpfileId());
					vouchers.add(voucher);
				}
				applyInfo.setVouchers(vouchers);
			}
			
			result.setApply(applyInfo);
		}
		
		if(take.getDevices() != null) {
			DevicesInfo devicesInfo = new DevicesInfo();
//			devicesInfo.setAlias(take.getDevices().getAlias());
			if(take.getDevices().getExpireDate() != null) {
				SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
				devicesInfo.setExpireDate(sdf1.format(take.getDevices().getExpireDate()));
			}
			devicesInfo.setDevicesCode(take.getDevices().getDevicesCode());
			devicesInfo.setDevicesId(take.getDevicesId());
			DevicesStatusEnum devicesStatus = DevicesStatusEnum.getByCode(take.getDevices().getDevicesStatus());
			
			if(devicesStatus != null) {
				devicesInfo.setStatusName(devicesStatus.getMessage());
			}
			
			result.setDevicesInfo(devicesInfo);
		}
		
		TakeStatusEnum takeStatus = TakeStatusEnum.getByCode(take.getTakeStatus());
		
		if(takeStatus != null) {
			result.setStatus(takeStatus.getCode());
			result.setStatusName(takeStatus.getMessage());
		}
		
		
		return result;
	}
	
	public static GetWaitUseDetailResponse valueOf(DevicesAct act) {
		GetWaitUseDetailResponse result = new GetWaitUseDetailResponse();
		
		if(act == null) {
			return result;
		}
		
		if(act.getDevices() != null) {
			result.setDevicesName(act.getDevices().getAlias());
		}
		result.setId(act.getDevicesActId());
		result.setType(ApplyTypeEnum.USED_APPLY.getCode());
		result.setTotal(act.getTotalTimes());
		result.setSurplus(act.getTotalTimes() - act.getUseTimes());
		result.setPartyId(act.getPartyId());
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if(act.getInvalidTime() != null) {
			result.setExpireTime(sdf.format(act.getInvalidTime()));
		}
		
		if(act.getSeal() != null) {
			SealInfo sealInfo = new SealInfo();
			sealInfo.setSealId(act.getSeal().getSealId());
			sealInfo.setSealName(act.getSeal().getSealName());
			if(StringUtils.isNotBlank(act.getSeal().getSealName())) {
				sealInfo.setIconText(act.getSeal().getSealName().length() > 2 ? act.getSeal().getSealName().substring(0, 2) : act.getSeal().getSealName().substring(0, 1));
			}
			sealInfo.setCustosPartyName(act.getSeal().getCustosName());
			sealInfo.setSealStatus(act.getSeal().getSealStatus());
			result.setSealInfo(sealInfo);
		}
		
		Apply apply = act.getApply();
		if(apply != null) {
			ApplyDetailInfo applyInfo = new ApplyDetailInfo();
			applyInfo.setPartyName(apply.getPerson().getName());
			applyInfo.setCause(act.getCause());
			
			ApplyStatusEnum applyStatus = ApplyStatusEnum.getByCode(apply.getApplyStatus());
			if(applyStatus != null) {
				applyInfo.setStatus(applyStatus.getCode());
				applyInfo.setStatusName(applyStatus.getMessage());
			}
			applyInfo.setTimes(apply.getApplyTimes());
			
			ApplyTypeEnum applyType = ApplyTypeEnum.getByCode(apply.getApplyType());
			
			applyInfo.setApplyType(act.getApplyType());
			if(applyType != null) {
				applyInfo.setApplyTypeName(applyType.getMessage());
			}
			
			if(apply.getApplyTime() != null) {
				applyInfo.setApplyTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(apply.getApplyTime()));
			}
			
			if(apply.getUpfiles() != null) {
				List<Voucher> vouchers = new ArrayList<Voucher>();
				
				for(Upfile upfile : apply.getUpfiles()) {
					if(upfile == null || StringUtils.isBlank(upfile.getFileUrl())) {
						continue;	
					}
					Voucher voucher = new Voucher();
					voucher.setUrl(upfile.getFileUrl());
					voucher.setId(upfile.getUpfileId());;
					vouchers.add(voucher);
				}
				applyInfo.setVouchers(vouchers);
			}
			
			result.setApply(applyInfo);
		}
		
		if(act.getDevices() != null) {
			DevicesInfo devicesInfo = new DevicesInfo();
//			devicesInfo.setAlias(act.getDevices().getAlias());
			if(act.getDevices().getExpireDate() != null) {
				SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
				devicesInfo.setExpireDate(sdf1.format(act.getDevices().getExpireDate()));
			}
			devicesInfo.setDevicesCode(act.getDevices().getDevicesCode());
			devicesInfo.setSign(act.getDevices().getSign());
			result.setDevicesInfo(devicesInfo);
		}
		
		return result;
	}
}
